/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kitaplaner.application;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import kitaplaner.database.Database;
import kitaplaner.database.Gruppe;
import kitaplaner.database.Kita;
import kitaplaner.database.Schicht;

/**
 *
 * @author kristina
 */
public class SchichtApp {
    Kita kita = new Kita();
    Database db = new Database();
    Connection conn;

    public SchichtApp(Kita kita, Connection conn)
    {
        this.conn = conn;
        this.kita = kita;
    }

    public Schicht getSchichtByID(int id)
    {
        Schicht schicht = null;
        ArrayList <Schicht> alSch = kita.getSchichten();

        for (int i = 0; i < alSch.size(); i++)
        {
            if (alSch.get(i).getId() == id)
            {
                schicht = alSch.get(i);
            }
        }

        return schicht;
    }

    public Schicht getSchichtByGrpIdSTET(int grpID, Time start, Time end)
    {
        Schicht schicht = null;
        ArrayList <Schicht> alSch = kita.getSchichten();

        for (int i = 0; i < alSch.size(); i++)
        {
            if (alSch.get(i).getGrpId() == grpID
             && alSch.get(i).getStart() == start
             && alSch.get(i).getEnd() == end)
            {
                schicht = alSch.get(i);
            }
        }

        return schicht;
    }

    public void insertIntoSchicht (int grpId, Time start, Time end, int maAnzMIN, int maAnzMAX)
    {
        Schicht schicht = new Schicht ();
        ArrayList <Schicht> alSchicht = kita.getSchichten();
        int size = alSchicht.size();

        schicht.setId(size);
        schicht.setEnd(end);
        schicht.setStart(start);
        schicht.setGrpId(grpId);
        schicht.setMaMax(maAnzMAX);
        schicht.setMaMin(maAnzMIN);

        alSchicht.add(schicht);
        kita.setSchichten(alSchicht);
    }

    public void changeSchichtEntry (int id, Time start, Time end, int maxMa, int minMa)
    {
        Schicht s = new Schicht ();
        ArrayList <Schicht> aS = kita.getSchichten();

        for (int i = 0; i < aS.size(); i++)
        {
            if (aS.get(i).getId() == id)
            {
                aS.get(i).setEnd(end);
                aS.get(i).setStart(start);
                aS.get(i).setMaMax(maxMa);
                aS.get(i).setMaMin(minMa);
            }
        }
        kita.setSchichten(aS);
    }

    public Boolean insertSchichtEntry (int id, Time start, Time end, int maxMa, int minMa)
    {
        Boolean compl = false;
        Schicht s = new Schicht ();
        
        s.setGrpId(id);
        s.setEnd(end);
        s.setMaMax(maxMa);
        s.setMaMin(minMa);
        s.setStart(start);

        conn = db.checkConnection(conn);
        compl = db.insertSchicht(conn, s);
        conn = db.checkConnection(conn);
        kita.setSchichten(db.getSchichten(conn));

        return compl;
    }

    public Boolean deleteSchichtEntry (int id)
    {
        Boolean success = false;
        ArrayList <Schicht> aS = kita.getSchichten();

        for (int i = 0; i < aS.size(); i ++)
        {
            if (aS.get(i).getId() == id)
            {
                aS.remove(i);
            }
        }

        conn = db.checkConnection(conn);
        success = db.deleteSchicht(conn, id);

        kita.setSchichten(aS);

        return success;
    }

    public Object [] createCompoboxText()
    {
        Object [] comboItems;

        ArrayList <Schicht> s = kita.getSchichten();
        ArrayList sNames = new ArrayList();

        for (int i = 0; i < s.size(); i++)
        {
            sNames.add(s.get(i).getId());
        }
        comboItems = sNames.toArray();

        return comboItems;
    }
    
    public Object [] createComboBoxTextByGrp(int grpID)
    {
        Object [] comboItems;

        ArrayList <Schicht> s = kita.getSchichten();
        ArrayList sNames = new ArrayList();

        for (int i = 0; i < s.size(); i++)
        {
            if (s.get(i).getGrpId() == grpID)
            {
                sNames.add(s.get(i).getId());
            }
        }
        comboItems = sNames.toArray();

        return comboItems;
    }

    public DefaultTableModel createSchichtOverview(String grpName)
    {
        String[] columnNames = {"ID", "Gruppen Name", "Startzeit", "Endzeit",
                                "Mitarbeiteranz. Min", "Mitarbeiteranz. Max"};
        ArrayList <Schicht> schicht = kita.getSchichten();
        ArrayList <Schicht> sgrp = new ArrayList <Schicht>();
        ArrayList <Gruppe> grp = kita.getGrp();
        DefaultTableModel model = new DefaultTableModel();
        JTable table = new JTable(model);
        int grpId = 0;

        for (int i = 0 ; i < grp.size() ; i ++)
        {
            if (grp.get(i).getName().equals(grpName))
            {
                grpId = grp.get(i).getId();
            }
        }

        //insert Columns
        for (int i = 0 ; i < columnNames.length; i++)
        {
            model.addColumn(columnNames[i]);
        }

        for (int i = 0; i < schicht.size(); i++)
        {
            if (schicht.get(i).getGrpId() == grpId)
            {
                sgrp.add(schicht.get(i));
            }
        }

        // sort Schichten
        sgrp = this.sortSchicht(sgrp);
        
        for (int i = 0; i < sgrp.size(); i++)
        {
            Object []obj = {sgrp.get(i).getId(), grpName,
                         sgrp.get(i).getStart(), sgrp.get(i).getEnd(),
                         sgrp.get(i).getMaMin(), sgrp.get(i).getMaMax()};
            model.insertRow (model.getRowCount(), obj);
        }
        return model;
    }


    public ArrayList <Schicht> sortSchicht (ArrayList <Schicht> alSchicht)
    {
        ArrayList <Schicht> alS = alSchicht;
        ArrayList <Schicht> dummy = new ArrayList <Schicht>();
        Schicht smallest = new Schicht();
        int small = 0;

        while (alS.size() > 0)
        {
          smallest = alS.get(0);
          small = 0;

          for (int i = 1; i < alS.size(); i++)
          {
             if (smallest.getStart().after(alS.get(i).getStart()))
             {
               smallest = alS.get(i);
               small = i;
             }
          }
          dummy.add(smallest);
          alS.remove(small);
        }
        return dummy;
    }
}
